<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<title>${tagName}</title>
    <link rel="stylesheet" type="text/css" href="${request.contextPath}/resources/css/custag.css">
</head>
<body>
	<div class="select-body">
		<!-- 问题选项 -->
	</div>
	<div class="footer-box">
		<button class="footer-confirm" onclick="saveCusTagAnswer()">确定</button>
	</div>
</body>
<script src="${request.contextPath}/resources/js/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="${request.contextPath}/resources/js/layer/layer.js" type="text/javascript"></script>
<script src="${request.contextPath}/resources/js/custag.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function(){
		var loadIndex = layer.load(0, {
	        shade: [0.1,'#fff'] //0.1透明度的白色背景
	    });
		$.when(loadCusTagSelect()).done(function(data){
			layer.closeAll('loading'); //关闭加载层
			console.log(data);
			if(data.success != true){
        		return false;
        	}
        	//组装html
        	for(var i = 0; i < data.entity.length; i++ ){
        		$(".select-body").append(loadCusTagSelectHtml(data.entity[i]));
        	}
        	//加载已选择的答案
        	loadCusTagAnswer();
		});
	});
		
	//查询数据
	function loadCusTagSelect(){
		var defer = $.Deferred();
		$.ajax({
			type: 'post',
	        url: "${request.contextPath}/appapi/getCusTagSelect",
	        data:{
	            "tagId": "${tagId}",
	        },
	        dataType: 'json',
	        success: function(data){
	        	defer.resolve(data);
		    }
		});
		return defer.promise();
	}
	
	//组装数据
	function loadCusTagSelectHtml(info){
		var html = '<div class="nav-select-box">';
		    html+= '<div class="nav-select-item multi-selected-box">';
		    html+= '<span>'+info.tvalue+'</span>';
		    html+= '<img id="'+info.id+'" class="multi-selected-img1" src="${request.contextPath}/resources/img/multiselected.png">';
		    html+= '<img class="multi-selected-img2" src="${request.contextPath}/resources/img/unselected.png">';
		    html+= '</div>';
		    html+= '</div>';
		return html;
	}
	
	//查询数据
	var ids = "";
	function loadCusTagAnswer(){
		$.ajax({
			type: 'post',
	        url: "${request.contextPath}/appapi/getCusTagAnswer",
	        data:{
	            "tagId": "${tagId}",
	            "extId": "${extId}",
	            "customerId": "${customerId}",
	        },
	        dataType: 'json',
	        success: function(data){
	        	if(data.entity == null){
	        		return false;
	        	}
	        	var info = data.entity[0];
	        	ids = info.id;
	        	tagValueId = info.tagValueId;
	        	var vId = tagValueId.split(",");
	        	$(".multi-selected-box").each(function() {
	    			var obj = $(this);
	    			var id = obj.children(".multi-selected-img1").attr("id");
	    			for(var i = 0; i < vId.length; i++){
	    				if(id == vId[i]){
	    					obj.children('span').addClass('nav-selected');
	    					obj.children(".multi-selected-img1").show();
	    					obj.children(".multi-selected-img2").hide();
	    				}
	    			}
	    		});
		    }
		});
	}
	
	//保存答案
	function saveCusTagAnswer(){
		//获取选中的选项ID
		var answerId = "";
		$(".multi-selected-img1").each(function() {
			var obj = $(this);
			if(obj.attr("style") == "display: inline;"){
				answerId += '"' + obj.attr("id") + '",';	
			}
		});
		if(answerId == ""){
			layer.msg("请至少选择一个选项");
			return false;
		}
		answerId = '[' + answerId.substring(0, answerId.length-1) + ']';
	    var json = {
	    	"questionId": "${extId}",
	    	"answerType": "2",
	    	"answerId": answerId,
	    	"select_other": "",
	    	"answerText": "",
	    	"tagId": "${tagId}"
		};
	    var array = new Array();
	    array.push(json);
		//设置遮罩层
		var loadIndex = layer.load(0, {
	        shade: [0.1,'#fff'] //0.1透明度的白色背景
	    });
		//保存问题
		$.ajax({
			type: 'post',
	        url: "${request.contextPath}/appapi/saveCusTagSelect",
	        data:{
	            "customerId": "${customerId}",
	            "userId": "${userId}",
	            "type": "1",
				"json": JSON.stringify(array),
				"ids": ids
	        },
	        dataType: 'json',
	        //async: false,
	        success: function(data){
	        	layer.closeAll('loading'); //关闭加载层
	        	if(data.success == true){
	        		layer.msg("保存成功");
	        		//延时执行执行回调方法并关闭窗口
                    setTimeout("callback()", 1000);
	        	}else{
	        		layer.msg(data.resMsg);
	        	}
	        }
		});
	}
	//返回上一页并刷新页面
	function callback(){
		//ios与安卓分开处理
		var ua = navigator.userAgent;
		if(ua.match(/iPhone|iPod|iPad/i) != null){
			window.location=document.referrer;
		}else{
			history.go(-1);
		}
		return false; 
	}
</script>
</html>